package smartvillage.controller;

import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import smartvillage.mapper.HouseholdMapper;
import smartvillage.mapper.OrderMapper;
import smartvillage.pojo.Household;
import smartvillage.pojo.Order;
import smartvillage.result.Result;
import smartvillage.utils.JWTUtil;
import smartvillage.vo.OrderVO;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrderController {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    HouseholdMapper householdMapper;

    //新增订单
    @PostMapping("/{orderId}")
    public Result addOrder(@PathVariable Integer orderId, HttpServletRequest request) {
        Order order = new Order();
        order.setCommodity(orderId);
        //1.获取购买者村户id
        String token = request.getHeader("token");
        Claims claims = JWTUtil.parseJWT("Rural", token);
        String tele = claims.get("tele").toString();  // 从JWT获取电话号码
        //根据户主电话号获取村户id
        Household householdByHolder = householdMapper.getHouseholdByHolder(tele);
        Integer houseId = householdByHolder.getId();
        order.setBuyer(houseId);
        //2.获取下单时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        order.setDatetime(formattedDateTime);
        //3、新增订单
        orderMapper.addOrder(order);
        return Result.success();
    }

    //查询自己是买家的订单
    @GetMapping("/buyer")
    public Result getOrderByBuyer(HttpServletRequest request){
        //1.获取村户id
        String token = request.getHeader("token");
        Claims claims = JWTUtil.parseJWT("Rural", token);
        String tele = claims.get("tele").toString();  // 从JWT获取电话号码
            //根据户主电话号获取村户id
        Household householdByHolder = householdMapper.getHouseholdByHolder(tele);
        Integer houseId = householdByHolder.getId();
        //2.查询订单
        List<OrderVO> orderByBuyer = orderMapper.getOrderByBuyer(houseId);
        return Result.success(orderByBuyer);
    }

    //查询自己是卖家的订单
    @GetMapping("/seller")
    public Result getOrderBySeller(HttpServletRequest request){
        //1.获取村户id
        String token = request.getHeader("token");
        Claims claims = JWTUtil.parseJWT("Rural", token);
        String tele = claims.get("tele").toString();  // 从JWT获取电话号码
        //根据户主电话号获取村户id
        Household householdByHolder = householdMapper.getHouseholdByHolder(tele);
        Integer houseId = householdByHolder.getId();
        //2.查询订单
        List<OrderVO> orderByBuyer = orderMapper.getOrderBySeller(houseId);
        return Result.success(orderByBuyer);
    }

    //成交订单
    @PutMapping("/{id}")
    public Result dealOrder(@PathVariable Integer id){
        orderMapper.dealOrder(id);
        return Result.success();
    }
}

